package com.ferry.transport.netty.lb;

import com.ferry.transport.api.Directory;
import com.ferry.transport.api.channel.WrappedChannelGroup;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Created by daiyong
 * 随机
 */
public class RandomLoadBalancer implements LoadBalancer{

	@Override
	public WrappedChannelGroup select(CopyOnWriteArrayList<WrappedChannelGroup> groupList) {

		if (groupList.size() == 0) {
			return null;
		}
		if (groupList.size() == 1) {
			return groupList.get(0);
		}

		ThreadLocalRandom random = ThreadLocalRandom.current();

		int n = random.nextInt(groupList.size() - 1);

		return groupList.get(n);
	}

}
